<template>
    <div class="page-bg">
        <div class="login-form30">
            <div class="login-title">注册</div>
            <div class="login-sub-title">Register</div>
            <el-form ref="form" :model="form" size="normal" :rules="rules">
                <el-form-item prop="name">
                    <el-input placeholder="请输入姓名" prefix-icon="el-icon-user" v-model="form.name"></el-input>
                </el-form-item>
                <el-form-item prop="password">
                    <el-input placeholder="请输入密码" prefix-icon="el-icon-lock" v-model="form.password" show-password></el-input>
                </el-form-item>
                <el-form-item prop="confirm">
                    <el-input placeholder="请确认密码" prefix-icon="el-icon-lock" v-model="form.confirm" show-password></el-input>
                </el-form-item>
                <el-form-item prop="address">
                    <el-input placeholder="请输入学院" prefix-icon="el-icon-user" v-model="form.address"></el-input>
                </el-form-item>
                <el-form-item prop="phoneNumber">
                    <el-input placeholder="请输入手机号" prefix-icon="el-icon-user" v-model="form.phoneNumber"></el-input>
                </el-form-item>
                <el-form-item prop="sex" v-if="this.role === '租客'">
                    <el-radio v-model="form.sex" label="男">男</el-radio>
                    <el-radio v-model="form.sex" label="女">女</el-radio>
                </el-form-item>
                <el-form-item prop="birthDate" v-if="this.role === '租客'">
                    <el-date-picker
                            placeholder="请选择出生日期"
                            value-format="YYYY-MM-DD"
                            style="width: 80%"
                            type="date"
                            clearable
                            v-model="form.birthDate"></el-date-picker>
                </el-form-item>
                <el-form-item>
                    <el-button style="width: 40%;margin-left: 20px" type="primary" @click="backLogin">返回</el-button>
                    <el-button style="width: 40%; float: right; margin-right: 20px" type="primary" @click="register">注册</el-button>
                </el-form-item>
            </el-form>
        </div>
    </div>
</template>

<script>
    import request from "../utils/request";

    export default {
        name: "UserRegister",
        data() {
            return{
                form: {
                    sex: '男',
                },
                role: '房主',
                rules: {
                    name: [
                        {required: true, message: '请输入姓名', trigger: 'blur'},
                    ],
                    password: [
                        {required: true, message: '请输入密码', trigger: 'blur'},
                    ],
                    confirm: [
                        {required: true, message: '请确认密码', trigger: 'blur'},
                    ],
                    address: [
                        {required: true, message: '请输入住址', trigger: 'blur'},
                    ],
                    phoneNumber: [
                        {required: true, message: '请输入手机号', trigger: 'blur'},
                    ],
                    birthDate: [
                        {required: true, message: '请输入出生日期', trigger: 'blur'},
                    ],
                }
            }
        },
        methods: {
            backLogin() {
                this.$router.push("/login");
            },
            register() {
                this.$refs['form'].validate((valid) => {
                    if (valid) {
                        if (this.form.password != this.form.confirm){
                            this.$message({
                                type:"error",
                                message: "两次密码输入不一致"
                            });
                            return;
                        }
                        if (this.role === '房主'){
                            this.form.role = 1;
                            this.form.birthDate = null;
                            this.form.sex = null;
                        }else{
                            this.form.role = 2;
                        }
                        request.post("/user/register", this.form).then(res => {
                            if (res.code == '0'){
                                this.$message({
                                    type:"success",
                                    message: "注册成功"
                                });
                                this.$router.push("/login");  //注册成功之后进行页面的跳转
                            } else {
                                this.$message({
                                    type:"error",
                                    message: res.msg
                                })
                            }
                        })
                    } else {
                        console.log('error submit!!')
                        return false
                    }
                })
            },
        }
    }
</script>

<style>
    .page-bg {
        width: 100%;
        height: 100vh;
        background-image: linear-gradient(#4338CA, #312E81);
        overflow: hidden;
    }

    .login-form30 {
        width: 400px;
        margin: 30px auto;
        padding: 2rem;
        flex: auto;
        justify-content: center;
        background-color: white;
        border-radius: 0.5rem;
        box-shadow: 10px 10px 15px rgba(0, 0, 0, 0.3);
    }

    .login-form50 {
        width: 400px;
        margin: 50px auto;
        padding: 2rem;
        background-color: white;
        border-radius: 0.5rem;
        box-shadow: 10px 10px 15px rgba(0, 0, 0, 0.3);
    }

    .login-title {
        color: #374151;
        font-size: 30px;
        text-align: center;
        padding: 10px 0px 0px 0px
    }

    .login-sub-title{
        font-size: 16px;
        text-align: center;
        padding: 0 0 20px 0;
        color: #9CA3AF;
    }

</style>